/**************************************
Regressions for Lee Bounds - Table A7

Runs the Lee Bounds regressions, then stores estimates in 
support_regs_leebounds_[date]

Author: Andrew Foote
Date: 02/04/2019


**************************************/
#delimit ;
set more off; 

include "./config.do" ;
cap log close; 
log using "./02.06.leebounds_$date.log", replace; 
use "$datadir/all_earnings_long.dta", clear; 

des;

tab year_grad;
include "./drbvars.do" ;
include "./leebounds.do" ;
include "./restrictions.do" ;

include "./top_school.do" ;


keep  if  year_grad >= 2001 & year_grad <=2013 ;
keep if year >= 2000 ;


tab flagship;
tab flagship year,m ;
tab opeid if flagship == 1 ;

/*********************************************************
Going to run a number of regressions (controlling for 
demographics) 

Outcomes: 

Total Earnings > 0
Log Earnings

**********************************************************/  

local demogs "white hispanic black asian texas" ;
local fixed_effects "texas i.year i.quarter i.year_grad" ;

replace instate_earnings = . if instate_earnings==0 ; 


/***************************************************
Lee Bounds Regressions, just for in-state
****************************************************/

keep if year>year_grad ;

/***********************
Setting up postfile
************************/

postfile support str5 spec year beta_in se_in lee_lower lee_upper N_top N_nottop share_trim using $supportdir/support_regs_leebounds_$date.dta, replace;  

reg instate_earnings top_school, cluster(pik);

local beta = _b[top_school];
local se = _se[top_school];

count if e(sample) & top_school == 1 ;
local treat = r(N) ;

count if e(sample) & top_school == 0 ;
local control = r(N) ;

count if top_school==1 ;
local treat_all=r(N) ; 

count if top_school==0 ; 
local control_all = r(N);

local share_trim = abs((`treat'/`treat_all') - (`control'/`control_all'));

di "SHARE TRIM =   " `share_trim' ;

leebounds instate_earnings top_school if year>year_grad,cieffect ;
end;
local bottom = e(cilower) ;
local top = e(ciupper) ;
local share =e(trim) ;

post support ("level") (0) (`beta') (`se') (`bottom') (`top') (`treat') (`control') (`share');

di e(trimmed) ;
di "SAMPLE SIZE AFTER TRIMMING";
di `treat'*(1-e(trim)) +`control';
di `control'*(1-e(trim)) + `treat' ;

di "IMPLICIT SAMPLE SIZE" ;
di `treat'*(1-2*e(trim)) ;
di `control'*(1-2*e(trim)) ;
           
reg log_earn_instate top_school, cluster(pik);

local beta = _b[top_school];
local se = _se[top_school];

count if e(sample) & top_school == 1 ;
local treat = r(N) ;

count if e(sample) & top_school == 0 ;
local control = r(N) ;

leebounds log_earn_instate top_school if year>year_grad,cieffect ;
local bottom = e(cilower) ;
local top = e(ciupper) ;
local share =e(trim) ;


post support ("logs") (0) (`beta') (`se') (`bottom') (`top') (`treat') (`control') (`share');


keep if year_grad <= 2006 ; 

foreach yrpost in 1 5 10 { ;

di "*************************************************";
di "**************** LEE BOUNDS FOR YEAR POST: `yrpost' ************" ;
di "**************************************************" ;
di "***** EARN ****" ;

   reg instate_earnings top_school if year-year_grad == `yrpost', cluster(pik) ;
            local beta = _b[top_school];
            local se = _se[top_school];
             
            count if e(sample) & top_school == 1 ;
            local treat = r(N) ;

            count if e(sample) & top_school == 0 ;
            local control = r(N) ;
                           
   leebounds instate_earnings top_school if year-year_grad == `yrpost' , cieffect;

                           ereturn list;  
               local bottom = e(cilower) ;
               local top = e(ciupper) ;
               local share =e(trim) ;

            post support ("level") (`yrpost') (`beta') (`se') (`bottom') (`top') (`treat') (`control') (`share');

                           
            di e(trimmed) ;
            di "SAMPLE SIZE AFTER TRIMMING";
            di `treat'*(1-e(trim)) +`control' ;
            di `control'*(1-e(trim)) + `treat' ;

            di "IMPLICIT SAMPLE SIZE" ;
            di `treat'*(1-2*e(trim)) ;
            di `control'*(1-2*e(trim)) ;               
di "***** LOG EARN *** " ;

   reg log_earn_instate top_school if year-year_grad == `yrpost' , cluster(pik) ;
            local beta = _b[top_school];
            local se = _se[top_school];
   
            count if e(sample) & top_school == 1 ;
            local treat = r(N) ;

            count if e(sample) & top_school == 0 ;
            local control = r(N) ;
                           
   leebounds log_earn_instate top_school if year-year_grad == `yrpost',cieffect ;
               local bottom = e(cilower) ;
               local top = e(ciupper) ;
               local share =e(trim) ;

            post support ("log") (`yrpost') (`beta') (`se') (`bottom') (`top') (`treat') (`control') (`share');

};                           

postclose support ;
